<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Support for Cooperative Multi-inheritance &#8212; PyQt 5.8.2 Reference Guide</title>
    
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '5.8.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="shortcut icon" href="_static/logo_tn.ico"/>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Things to be Aware Of" href="gotchas.html" />
    <link rel="prev" title="Integrating Python and QML" href="qml.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="gotchas.html" title="Things to be Aware Of"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="qml.html" title="Integrating Python and QML"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">PyQt 5.8.2 Reference Guide</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="support-for-cooperative-multi-inheritance">
<span id="ref-cooperative-multiinheritance"></span><h1>Support for Cooperative Multi-inheritance<a class="headerlink" href="#support-for-cooperative-multi-inheritance" title="Permalink to this headline">¶</a></h1>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This section is not about sub-classing from more that one Qt class.</p>
</div>
<p>Cooperative multi-inheritance is a technique for implementing classes that
inherit multiple super-classes - typically a main super-class and one or more
mixin classes that add additional behaviour.  It makes it easy to add new
mixins at a later date to further extend the behavior, without needing to
change either the implementation of the class or any existing code that creates
an instance of the class.</p>
<p>The technique requires that all the super-class&#8217;s <code class="docutils literal"><span class="pre">__init__</span></code> methods follow
the same pattern in the way that they handle unrecognised keyword arguments and
use <code class="docutils literal"><span class="pre">super()</span></code> to invoke their own super-class&#8217;s <code class="docutils literal"><span class="pre">__init__</span></code> methods.</p>
<p>PyQt5&#8217;s classes follow this pattern.</p>
<p>See Raymond Hettinger&#8217;s <a class="reference external" href="http://rhettinger.wordpress.com/2011/05/26/super-considered-super/">Python&#8217;s super() considered super!</a> blog
post for some more background on the subject.</p>
<p>As an example, let&#8217;s say we have a class that represents a person, and that a
person has a name.  The following might be an initial implementation:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">QObject</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
        <span class="n">QObject</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
</pre></div>
</div>
<p>An instance would normally be created in one of the following ways:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">person</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s2">&quot;Joe&quot;</span><span class="p">)</span>
<span class="n">person</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s2">&quot;Joe&quot;</span><span class="p">,</span> <span class="n">some_parent</span><span class="p">)</span>
</pre></div>
</div>
<p>This approach has some limitations:</p>
<ul class="simple">
<li>Only a sub-set of the <a class="reference internal" href="api/qobject.html#PyQt5.QtCore.QObject" title="PyQt5.QtCore.QObject"><code class="xref py py-class docutils literal"><span class="pre">QObject</span></code></a> API is exposed.  For
example you cannot set the value of a Qt property or connect a signal by
passing appropriate keyword arguments to <code class="docutils literal"><span class="pre">Person.__init__</span></code>.</li>
<li>Adding another class to <code class="docutils literal"><span class="pre">Person</span></code>&#8216;s list of super-classes means that its
<code class="docutils literal"><span class="pre">__init__</span></code> implementation needs to be changed.  If the new mixin takes
non-optional arguments then every call to create a <code class="docutils literal"><span class="pre">Person</span></code> instance will
need changing.</li>
</ul>
<p>Consider this alternative implementation:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">QObject</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwds</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
</pre></div>
</div>
<p>The difference is that we only handle arguments that are used by the <code class="docutils literal"><span class="pre">Person</span></code>
class itself and we punt all the other arguments to the super-classes by
calling <code class="docutils literal"><span class="pre">super()</span></code>.</p>
<p>With this implementation an instance would normally be created in one of the
following ways:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">person</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s2">&quot;Joe&quot;</span><span class="p">)</span>
<span class="n">person</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s2">&quot;Joe&quot;</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="n">some_parent</span><span class="p">)</span>
</pre></div>
</div>
<p>Here the difference is that we are using keyword arguments to specify any
arguments that are not handled by the <code class="docutils literal"><span class="pre">Person</span></code> class itself.  Note that we
could use keyword arguments for all arguments - whether or not you do so is
down to personal choice.</p>
<p>The limitations of the first implementation no longer apply.  For example,
without any further changes we can also do this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">person</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s2">&quot;Joe&quot;</span><span class="p">,</span> <span class="n">destroyed</span><span class="o">=</span><span class="n">some_callable</span><span class="p">)</span>
</pre></div>
</div>
<p>Let&#8217;s say we now want to extend the behaviour of the <code class="docutils literal"><span class="pre">Person</span></code> class by adding
a mixin that handles a person&#8217;s age.  The implementation of the mixin would be
as follows:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Age</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">age</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwds</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">age</span> <span class="o">=</span> <span class="n">age</span>
</pre></div>
</div>
<p>This follows a similar pattern to our <code class="docutils literal"><span class="pre">Person</span></code> implementation, but notice
that we have provided the <code class="docutils literal"><span class="pre">age</span></code> argument with a default value.</p>
<p>The following is our new <code class="docutils literal"><span class="pre">Person</span></code> implementation:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">QObject</span><span class="p">,</span> <span class="n">Age</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwds</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
</pre></div>
</div>
<p>The only change we have had to make is to add <code class="docutils literal"><span class="pre">Age</span></code> to <code class="docutils literal"><span class="pre">Person</span></code>&#8216;s list of
super-classes.  More importantly we do not need to change any call to create a
<code class="docutils literal"><span class="pre">Person</span></code> instance.</p>
<p>If we do want to create a <code class="docutils literal"><span class="pre">Person</span></code> instance with a non-default age then we
simply pass it as a keyword argument as follows:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">person</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s2">&quot;Joe&quot;</span><span class="p">,</span> <span class="n">age</span><span class="o">=</span><span class="mi">38</span><span class="p">)</span>
</pre></div>
</div>
<p>This technique increases the use of keyword arguments - while this means a bit
more typing, it significantly increases the readability of application code.</p>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/logo.png" alt="Logo"/>
            </a></p>
  <h4>Previous topic</h4>
  <p class="topless"><a href="qml.html"
                        title="previous chapter">Integrating Python and QML</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="gotchas.html"
                        title="next chapter">Things to be Aware Of</a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="gotchas.html" title="Things to be Aware Of"
             >next</a> |</li>
        <li class="right" >
          <a href="qml.html" title="Integrating Python and QML"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">PyQt 5.8.2 Reference Guide</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2015 Riverbank Computing Limited.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.3.
    </div>
  </body>
</html>